package com.learn.im.domain;

import org.springframework.http.HttpStatus;

import java.util.HashMap;

/**
 * 返回实体类
 * 成功统一返回200 失败返回500
 * @author Emp
 * @version :    1.0
 */
public class R extends HashMap<String,Object> {
    private static final long serialVersionUID = 1L;

    public static final String MSG = "msg";
    public static final String DATA = "data";
    public static final String CODE = "code";

    public R(){}

    public R(int code,String msg){
        super.put(CODE,code);
        super.put(MSG,msg);
    }

    public R(int code,Object data,String msg){
        super.put(CODE,code);
        super.put(DATA,data);
        super.put(MSG,msg);
    }

    public static R ok(Object data){
        return ok("操作成功",data);
    }

    public static R ok(String msg){
        return ok(msg,null);
    }

    public static R ok(String msg,Object data){
        return new R(HttpStatus.OK.value(), data,msg);
    }

    public static R error(){
        return error("操作失败");
    }

    public static R error(String msg){
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(),msg);
    }

    public static R error(int code,String msg){
        return new R(code,msg);
    }

    public R put(String key, Object value){
        super.put(key,value);
        return this;
    }
}
